package com.c611.smartclassroom.service.Impl;

import com.c611.smartclassroom.mapper.BuildMapper;
import com.c611.smartclassroom.mapper.SchoolMapper;
import com.c611.smartclassroom.model.Build;
import com.c611.smartclassroom.model.BuildExample;
import com.c611.smartclassroom.model.School;
import com.c611.smartclassroom.service.BuildService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class BuildServiceImpl implements BuildService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    BuildMapper buildMapper;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    SchoolMapper schoolMapper;

    @Cacheable(value = "build",key = "'queryAllBuildCount::' + #schoolid")
    @Override
    public long queryAllBuildCount(String schoolid) {
        BuildExample buildExample = new BuildExample();
        buildExample.createCriteria().andDelflagEqualTo(0).andSchoolidEqualTo(schoolid);
        return buildMapper.countByExample(buildExample);
    }

    /**
     * 通过学校ID，查询该学校的所有教学楼
     * @param schoolID
     * @return List<Build>
     */
    @Cacheable(value = "build",key = "'queryAllBuild::' + #schoolID + '::' + #tbuildname + '::' + #page + '::' + #pageSize",unless = "#result == null ")
    @Override
    public List<Build> queryAllBuild(String schoolID,String tbuildname, Integer page, Integer pageSize) {

        //按页查询
        RowBounds rowBounds = new RowBounds((page - 1) * pageSize,pageSize);
        //构造器
        BuildExample buildExample = new BuildExample();
        if (tbuildname == null){
            buildExample.createCriteria().andSchoolidEqualTo(schoolID).andDelflagEqualTo(0);
        }else {
            //tbuildname模糊查询
            buildExample.createCriteria().andSchoolidEqualTo(schoolID).andTbuildnameLike("%"+tbuildname+"%").andDelflagEqualTo(0);
        }
        //调用schoolMapper查询schoolID对应的学校名称
        School school = schoolMapper.selectByPrimaryKey(schoolID);
        if (school == null) return null;
        String schoolName= school.getSchoolname();

        //用List接受查询数据，在对数据中的schoolID进行替换成对应的学校名称
        List<Build> buildList = buildMapper.selectByExampleWithRowbounds(buildExample,rowBounds);
        if (buildList == null) return null;
        for (int i = 0; i < buildList.size(); i++) {
            buildList.get(i).setSchoolid(schoolName);
        }
        return buildList;
    }

    /**
     * 根据教学楼ID，编辑教学楼信息
     * @param
     * @return
     */
    @CacheEvict(value = {"build","school::build","build::class","school::build::class"},allEntries = true)
    @Override
    public int saveBuild(Build build) {

        return buildMapper.updateByPrimaryKeySelective(build);
    }

    /**
     * 添加教学楼
     * @param build
     * @return
     */
    @CacheEvict(value = {"build","school::build","build::class","school::build::class"},allEntries = true)
    @Override
    public int addBuild(Build build) {

        build.setAddtime(new Date());
        build.setDelflag(0);
        return buildMapper.insert(build);
    }

    /**
     * 根据教学楼ID ，删除教学楼
     * @param tbuildid
     * @return
     */
    @CacheEvict(value = {"build","school::build","build::class","school::build::class"},allEntries = true)
    @Override
    public int delBuild(String tbuildid) {

        Build build = new Build();
        build.setDelflag(1);
        build.setTbuildid(tbuildid);
        return buildMapper.updateByPrimaryKeySelective(build);
    }

    @Cacheable(value = "school",key = "'querySchoolName::' + #result",unless = "#result == null ")
    @Override
    public String querySchoolName(String schoolID) {
        School school = schoolMapper.selectByPrimaryKey(schoolID);
        return school.getSchoolname();
    }

    @Cacheable(value = "build",key = "'queryBuildID::' + #result + '::' + #buildName",unless = "#result == null ")
    @Override
    public String queryBuildID(String SchoolID, String buildName) {
        BuildExample buildExample = new BuildExample();
        buildExample.createCriteria().andSchoolidEqualTo(SchoolID).andTbuildnameEqualTo(buildName);
        List<Build> buildList =  buildMapper.selectByExample(buildExample);
        Build build = buildList.get(0);
        return build.getTbuildid();
    }

    @Cacheable(value = "build",key = "'checkBuildId::' + #tbuildid",unless = "#result == null ")
    @Override
    public int checkBuildId(String tbuildid) {
        Build build = buildMapper.selectByPrimaryKey(tbuildid);
        if(build != null) return 1;
        return 0;
    }


}
